package com.heima.wemedia.task;
// 爱情不是随便许诺,算了,不想再说了

import com.heima.common.delayTask.RedisDelayedQueueUtil;
import com.heima.wemedia.service.WmNewsAuditService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class RedisDelayedQueueInit {


    @Autowired
    private RedisDelayedQueueUtil redisDelayedQueueUtil;
    @Autowired
    private WmNewsAuditService wmNewsAuditService;

    @PostConstruct
    public void init(){
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);

        executorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    Object obj = redisDelayedQueueUtil.getDelayQueue("wm.news.pub");
                    Integer wmUserId = Integer.valueOf(obj.toString());
                    // 调用发布方法
                    wmNewsAuditService.publisWmNews(wmUserId,null);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },0,1, TimeUnit.SECONDS);


        // 初始化完成后,向队列添加一个无用的元素
        redisDelayedQueueUtil.addQueue(0,1,TimeUnit.SECONDS,"wm.news.pub");
    }
}
